package net.homeip.jcperera.nu.beans;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;

import net.homeip.jcperera.nu.model.navigation.NavigationElement;

import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@ManagedBean
@SessionScoped
public class NavigationController implements Serializable
{
    // Required to implement the Serializable interface
    private static final long serialVersionUID = -5401837658085079450L;

    private TreeNode          root;
    private TreeNode          selected;

    public NavigationController()
    {
        root = new DefaultTreeNode("root", null);
        TreeNode leaf;

        TreeNode data = new DefaultTreeNode("folder", new NavigationElement("data", "Data", "Folder"), root);
        TreeNode configuration = new DefaultTreeNode("folder", new NavigationElement("configuration", "Configuration",
                "Folder"), root);
        TreeNode system = new DefaultTreeNode("folder", new NavigationElement("system", "System", "Folder"), root);

        // Data
        TreeNode history = new DefaultTreeNode(new NavigationElement("history", "Scan History", "Scan History",
                "/snippets/data/history"), data);
        TreeNode cases = new DefaultTreeNode(new NavigationElement("cases", "Scanned Cases", "Scanned Cases",
                "/snippets/data/cases"), data);

        // Configuration
        TreeNode webinfosite = new DefaultTreeNode("folder", new NavigationElement("webinfosite",
                "Web Info Site Administration", "Folder"), configuration);

        TreeNode webinfositelist = new DefaultTreeNode(new NavigationElement("webinfositelist", "Web Info Site List",
                "List Web Info Sites", "/snippets/configuration/webinfosite/list"), webinfosite);

        TreeNode addnewwebinfosite = new DefaultTreeNode(new NavigationElement("addnewwebinfosite",
                "Web Info Site Add", "Add Web Info Site", "/snippets/configuration/webinfosite/edit"), webinfosite);

        // System
        TreeNode about = new DefaultTreeNode(new NavigationElement("about", "About", "About this System",
                "/snippets/system/about"), system);
        TreeNode systemlogs = new DefaultTreeNode(new NavigationElement("systemlogs", "Consult System Logs",
                "Consult System Logs", "/snippets/system/logs"), system);

        selected = addnewwebinfosite;
    }

    /**
     * Gets the <code>URL</code> associated to a {@link TreeNode} if exists. Otherwise returns an empty {@link String}
     * (never <code>null</code>)
     * 
     * @param node
     *            The {@link TreeNode} to get the URL from the {@link NavigationElement}
     * @return the value or an empty {@link String}
     */
    public static String getURL(TreeNode node)
    {
        String result = "";
        NavigationElement nav = (NavigationElement) node.getData();
        if (nav != null) result = nav.getURL().trim();
        return result;
    }

    public void onNodeSelect(NodeSelectEvent event)
    {
        TreeNode selection = event.getTreeNode();
        if (getURL(selection).length() > 0) selected = selection;
    }

    public TreeNode getRoot()
    {
        return root;
    }

    public TreeNode getSelected()
    {
        return selected;
    }

    public void setSelected(TreeNode selected)
    {
        this.selected = selected;
    }

    public String getURL()
    {
        return getURL(selected);
    }
}
